package com.troila.cloud.simplify.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.troila.cloud.simplify.model.STitle;
import com.troila.cloud.simplify.service.STitleService;

@Controller
@RequestMapping("/")
public class PageController {
	@Autowired
	private STitleService sTitleService;
	@Value("${question.domain}")
	private String domain;

	
	@GetMapping("front/page")
	public String index(HttpSession session,Model model,HttpServletRequest request, HttpServletResponse response) {
		String mediaInfo = request.getAttribute("mediaInfo").toString();
		JSONObject  miJson = JSONObject.parseObject(mediaInfo);
		session.setAttribute("avatarImage", miJson.getString("avatar_image"));
		session.setAttribute("mediaId", miJson.getString("media_id"));
//		System.out.println("avatar_image:"+miJson.getString("avatar_image"));
//		System.out.println("mediaInfo:"+mediaInfo);
//		session.setAttribute("avatarImage", "123");
//		session.setAttribute("mediaId", "gh_43d7956970ca");
		return "/front/list_question";
	}
	@GetMapping("front/question")
	public String question(HttpSession session,Model model,@RequestParam String titleId) {
		session.setAttribute("titleId", titleId);
		STitle st = sTitleService.findById(Integer.valueOf(titleId));
		if(st != null) {
			session.setAttribute("mediaId", st.getMediaKey());
			session.setAttribute("title", st.gettName());
			session.setAttribute("logo", st.gettLogo());
			session.setAttribute("domain", domain);
			if(!StringUtils.isEmpty(st.getShareTitle())) {
				session.setAttribute("shareTitle", st.getShareTitle());
			}
			if(!StringUtils.isEmpty(st.getShareUrl())) {
				session.setAttribute("shareUrl", st.getShareUrl());
			}
			
		}else {
			session.setAttribute("title", "魔镜魔镜");
		}
		return "/front/question";
	}
	

}
